package com.longjianghuasheng.logistics.business.inquiry

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.setFragmentResult
import com.blankj.utilcode.util.LogUtils
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseTitleFragment
import com.longjianghuasheng.logistics.business.home.TypeResponse
import com.longjianghuasheng.logistics.business.inquiry.adapter.RootNodeProvider
import com.longjianghuasheng.logistics.business.inquiry.adapter.SecondNodeProvider
import com.longjianghuasheng.logistics.business.inquiry.adapter.ServiceSelectionAdapter
import com.longjianghuasheng.logistics.business.inquiry.adapter.ThirdNodeProvider
import com.longjianghuasheng.logistics.databinding.FragmentServiceSelectionBinding
import com.longjianghuasheng.logistics.ext.initDivide
import com.longjianghuasheng.logistics.ext.observer
import me.hgj.jetpackmvvm.ext.view.visible

class ServiceSelectionFragment :
    BaseTitleFragment<ServiceSelectionViewModel, FragmentServiceSelectionBinding>() {

    private val serviceSelectionAdapter by lazy {
        ServiceSelectionAdapter()
    }

    override fun layoutId(): Int = R.layout.fragment_service_selection

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        initRecycler()
        initListener()
    }

    private fun initListener() {
        mDatabind.btnConfirm.setOnClickListener {
            val list = arrayListOf<ThirdNode>()
            serviceSelectionAdapter.data.forEach {

                it.childNode?.forEach {
                    it.childNode?.forEach {

                        list.add(it as ThirdNode)
                    }
                }

            }

            setFragmentResult(mViewModel.type, Bundle().apply {


                putParcelableArray("service",
                    list.filter { it.isSelect }
                        .map { TypeResponse(title = it.title, id = it.id, types = mutableListOf()) }
                        .toTypedArray()
                )


            })
            navigateUp()

        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initRecycler() {

        serviceSelectionAdapter.run {


            addNodeProvider(RootNodeProvider { firstNode: FirstNode, i: Int, isChecked: Boolean ->
                firstNode.childNode?.forEach { second ->
                    val secondNode = second as SecondNode
                    secondNode.isSelect = isChecked
                    secondNode.childNode?.forEach { third ->
                        val thirdNode = third as ThirdNode
                        thirdNode.isSelect = isChecked
                    }
                }
                notifyDataSetChanged()

            })
            addNodeProvider(SecondNodeProvider { secondNode: SecondNode, i: Int, isChecked: Boolean ->


                LogUtils.d("node====================$i")

                val firstNode = data[i] as FirstNode

                secondNode.childNode?.forEach { third ->
                    val thirdNode = third as ThirdNode
                    thirdNode.isSelect = isChecked
                }

                firstNode.isSelect = firstNode.childNode?.all {

                    (it as SecondNode).isSelect

                } ?: false

                notifyDataSetChanged()


            })

            addNodeProvider(ThirdNodeProvider
            { thirdNode: ThirdNode, i: Int, isChecked: Boolean ->

                LogUtils.d("node====================$i")

                val secondNode = data[i] as SecondNode

                secondNode.isSelect = secondNode.childNode?.all {
                    (it as ThirdNode).isSelect
                } ?: false


                val firstNode = data.first { rootNode ->

                    rootNode.childNode?.contains(secondNode) ?: false

                }

                (firstNode as FirstNode).isSelect = firstNode.childNode?.all {

                    (it as SecondNode).isSelect
                } ?: false

                notifyDataSetChanged()
            })

        }

        mDatabind.rvServer.run {
            initDivide(bindAdapter = serviceSelectionAdapter)
            setHasFixedSize(false)
        }


    }

    override fun createObserver() {
        super.createObserver()
//        mViewModel.result.observer(this) {
//
//            serviceSelectionAdapter.setNewInstance(it.toMutableList())
//
//        }
        mViewModel.convertResult.observer(this) {
            serviceSelectionAdapter.setNewInstance(it?.toMutableList())

            mDatabind.llContainer.visible()
        }
    }

    override fun initData() {
        super.initData()
        mViewModel.type = arguments?.getString("type") ?: ""

        initTitleBar(if (mViewModel.type == InquiryFragment.KEY_SERVICE1) "保税服务" else "附加服务")
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.service_list()
    }
}